$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var all_datagrid =  $('#all_datagrid');

    //数据表格
    all_datagrid.datagrid({
        url:'/linkMan/list.do',
        title:"联系人管理",
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'name',title:'联系人名称',width:100},
            {field:'sex',title:'性别',width:100,formatter:sexFormatter},
            {field:'clientSchool',title:'客户所在学校',width:100,formatter:linkManSchoolFormatter},
            {field:'department',title:'部门',width:100},
            {field:'duty',title:'岗位',width:100},
            {field:'phone',title:'电话',width:100},
            {field:'qq',title:'QQ',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'main',title:'是否主要联系人',width:100},
        ]]
    });
    //end



    var cmdObj = {

        //重置
        clear:function(){
            edit_form.form("clear");
        },

        query : function () {
            var param = $("#keyword").val();
            var clientId = $("#clientId").val();

            all_datagrid.datagrid('load',{
                keyword: param,
                clientId: clientId,
            });
        },

        //点击添加按钮,添加联系人
        add : function () {
            edit_dialog.dialog("setTitle","添加联系人");
            edit_form.form("clear");


            edit_dialog.dialog("open");
        },
        //刷新
        reload : function () {
            all_datagrid.datagrid("reload");
        },
        //编辑按钮编辑联系人
        edit : function () {

            //显示按钮
            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();

            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的联系人","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑联系人");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            if(row.clientSchool){
                row["clientSchool.id"]=row.clientSchool.id;
            }


            edit_form.form("load",row);

            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#hiddenId").val();

            if(id){
                url = "/linkMan/update.do";
            }else{
                url = "/linkMan/save.do";
            }

            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            all_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //删除联系人
        remove : function () {
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的联系人","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/linkMan/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },

        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        }
    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: "60%",
        height: "80%"
    });
    //end


})//end




//客户学校格式化
function linkManSchoolFormatter(value,row,index) {
    if (value == null){
        return;
    }
    return value.name;
}

function sexFormatter(value,row,index) {
    return value ? "男" : "女";
}